{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.6 \u5b57\u7b26\u4e32\u7684I/O\u64cd\u4f5c\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u4f7f\u7528\u64cd\u4f5c\u7c7b\u6587\u4ef6\u5bf9\u8c61\u7684\u7a0b\u5e8f\u6765\u64cd\u4f5c\u6587\u672c\u6216\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 io.StringIO() \u548c io.BytesIO() \u7c7b\u6765\u521b\u5efa\u7c7b\u6587\u4ef6\u5bf9\u8c61\u64cd\u4f5c\u5b57\u7b26\u4e32\u6570\u636e\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = io.StringIO()\ns.write('Hello World\\n')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print('This is a test', file=s)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Get all of the data written so far\ns.getvalue()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Wrap a file interface around an existing string\ns = io.StringIO('Hello\\nWorld\\n')\ns.read(4)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.read()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "io.StringIO \u53ea\u80fd\u7528\u4e8e\u6587\u672c\u3002\u5982\u679c\u4f60\u8981\u64cd\u4f5c\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u8981\u4f7f\u7528 io.BytesIO \u7c7b\u6765\u4ee3\u66ff\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = io.BytesIO()\ns.write(b'binary data')\ns.getvalue()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4f60\u60f3\u6a21\u62df\u4e00\u4e2a\u666e\u901a\u7684\u6587\u4ef6\u7684\u65f6\u5019 StringIO \u548c BytesIO \u7c7b\u662f\u5f88\u6709\u7528\u7684\u3002\n\u6bd4\u5982\uff0c\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 StringIO \u6765\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6d4b\u8bd5\u6570\u636e\u7684\u7c7b\u6587\u4ef6\u5bf9\u8c61\uff0c\n\u8fd9\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u88ab\u4f20\u7ed9\u67d0\u4e2a\u53c2\u6570\u4e3a\u666e\u901a\u6587\u4ef6\u5bf9\u8c61\u7684\u51fd\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c StringIO \u548c BytesIO \u5b9e\u4f8b\u5e76\u6ca1\u6709\u6b63\u786e\u7684\u6574\u6570\u7c7b\u578b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002\n\u56e0\u6b64\uff0c\u5b83\u4eec\u4e0d\u80fd\u5728\u90a3\u4e9b\u9700\u8981\u4f7f\u7528\u771f\u5b9e\u7684\u7cfb\u7edf\u7ea7\u6587\u4ef6\u5982\u6587\u4ef6\uff0c\u7ba1\u9053\u6216\u8005\u662f\u5957\u63a5\u5b57\u7684\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}